package hlhj.fhp.checkpoint.fgms

import android.content.Intent
import android.support.v7.app.AlertDialog
import android.util.Log
import com.bumptech.glide.Glide
import com.example.mymvp.log
import com.example.mymvp.mvp.BaseFgm
import hlhj.fhp.checkpoint.Constant
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.atys.*
import hlhj.fhp.checkpoint.http.Urls
import hlhj.fhp.checkpoint.iview.HomeSelfView
import hlhj.fhp.checkpoint.javaBean.MyBaseBean
import hlhj.fhp.checkpoint.javaBean.UserInfoBean
import hlhj.fhp.checkpoint.presenter.HomeSelfPresenter
import hlhj.fhp.checkpoint.utils.GlideUtils
import hlhj.fhp.checkpoint.utils.SPHelper
import kotlinx.android.synthetic.main.fgm_home_self.view.*

/**
 * Created by Never Fear   on 2018\11\7 0007.
Never More....
 */
class HomeSelfFgm :BaseFgm<HomeSelfView,HomeSelfPresenter>(), HomeSelfView {
    private var nick:String?=""
    private var head:String?=""
    private var isLoadOut=true
    override fun setUserInfo(userInfoBean: UserInfoBean) {
        Constant.isActive=userInfoBean.info.isIs_active
        Constant.Active_Money=userInfoBean.info.active_price
        if (isLoadOut){
            Glide.with(context).load(Urls.ImgUrl+userInfoBean.info.head_img)
                    .apply(GlideUtils.loadHeadOption()!!)
                    .into(rootView.userHead)
        }
        isLoadOut=true
        nick=userInfoBean.info.nickname
        head=userInfoBean.info.head_img
        rootView.tvName.text=userInfoBean.info.nickname
        rootView.tvMoney.text=userInfoBean.info.balance.toString()

        when(userInfoBean.info.status){
            0->{
                rootView.tvStatus.text="未认证"
                rootView.btIdentity.isClickable=true
            }
            1->{
                rootView.tvStatus.text="认证中"
                rootView.btIdentity.isClickable=false
            }
            2->{
                rootView.tvStatus.text="认证通过"
                rootView.btIdentity.isClickable=false
            }
            3->{
                rootView.tvStatus.text="认证未通过"
                rootView.btIdentity.isClickable=true
            }
        }
    }

    override fun initView() {
    }

    override fun logicStart() {
        presenter?.getUserInfo(context!!)
    }

    private val GOINFO: Int=1234

    override fun setListener() {
        rootView.tvMoney.setOnClickListener {
            val intent = Intent(context,WalletAty::class.java)
            startActivity(intent)
        }
        rootView.btVip.setOnClickListener {
            val intent = Intent(context,VipAty::class.java)
            startActivity(intent)
        }
        rootView.loHead.setOnClickListener {
            val intent = Intent(context,UserInfoAty::class.java)
            intent.putExtra("head",Urls.ImgUrl+head)
            intent.putExtra("nick",nick)

            activity?.startActivityFromFragment(this,intent,GOINFO)
        }
        rootView.btIdentity.setOnClickListener {
            val intent = Intent(context,IdentityAty::class.java)
            activity?.startActivityFromFragment(this,intent,1)
        }
        rootView.btShare.setOnClickListener {
            val intent = Intent(context,ShareAty::class.java)
            startActivity(intent)
        }
        rootView.btStatement.setOnClickListener {
            val intent = Intent(context,StatementAty::class.java)
            startActivity(intent)
        }
        rootView.btProblem.setOnClickListener {
            val intent = Intent(context,ProblemAty::class.java)
            startActivity(intent)
        }

        rootView.btReport.setOnClickListener {
            val intent = Intent(context,ReportCenterAty::class.java)
            startActivity(intent)
        }
        rootView.btUnLogin.setOnClickListener {
            AlertDialog.Builder(context!!)
                    .setTitle("确认退出当前账户？")
                    .setPositiveButton("确定"){
                        dialog, which ->
                        dialog.dismiss()
                        presenter?.loginOut(context!!)
                    }
                    .setNegativeButton("取消"){
                        dialog, which ->
                        dialog.dismiss()
                    }
                    .show()
        }
    }

    override fun setLoginOut(myBaseBean: MyBaseBean) {
        SPHelper.saveToken(context!!,"")
        val intent = Intent(context, LoginAty::class.java)
        startActivity(intent)
        activity?.finish()
    }

    override fun bindView(): HomeSelfView {
        return this
    }

    override fun createPresenter(): HomeSelfPresenter {
        return HomeSelfPresenter()
    }

    override fun doBeforInit() {
    }

    override fun getContentId(): Int {
        return R.layout.fgm_home_self
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        log("","到没到信息FGM${requestCode}")
        if (resultCode!=-1)return
        when(requestCode){
            GOINFO->{
                val extra = data?.getStringExtra("icon")
                log("","到没到信息FGM${extra==null}")
                extra?.let {
                    Glide.with(context).load(it)
                            .into(rootView.userHead)
                    isLoadOut=false
                }
            }
        }
        presenter?.getUserInfo(context!!)
    }
}